$(function(){
	loadBillList();
	selectItemListChecked(false);
	selectItemBrands();
	selectItemCatTree();
});

/**
 * 加载发票列表
 */
function loadBillList(){
	$('#ticketList_dateGrid_checked').datagrid({
		loadMsg : "数据加载中",
		title:"所有开票资料",
		border:true,
		fit : true,
		fitColumns : true,
		pagination : true,
		rownumbers : true,
		singleSelect : false,
		queryParams : {},
		toolbar : "#itemlist_dategrid_toolbar",
		columns : [ [
		    {field:	 'checked',checkbox:true},
		    {field : 'id',hidden : true},
		    {field : 'company',hidden : true},
		    {field : 'taxno',hidden : true},
		    {field : 'address',hidden : true},
		    {field : 'mobile',hidden : true},
		    {field : 'bankno',hidden : true},
		    {field : 'imageurl',hidden : true},
		    {field : 'receiver',hidden : true},
		    {field : 'receiver_phone',hidden : true},
		    {field : 'province',hidden : true},
		    {field : 'city',hidden : true},
		    {field : 'area',hidden : true},
		    {field : 'receiver_address',hidden : true},
		    {field : 'createdate',hidden : true},
			{field : 'orderNum',	title : '记录号',	width : 80,align : 'center',
				formatter: function(value,row,index){
					console.log(IMAGE_BASE_URL+row.imageUrl1);
					return 	'<div style="width:50%;height:100%;float:left;margin-left: 0px;padding-left:0px; text-align:left"><ul style="float:left"><li><span>'+row.buyerid+'</span></li></ul></div>';
				}
			},
			{field : 'buyerName',	title : '用户名',		width : 100,align : 'center',
				formatter: function(value,row,index){
					return 	value;
				}
			},
			{field : 'level',title : '会员等级',	width : 40,align : 'center',hidden : false,
				formatter: function(value,row,index){
					return 	value;
				},
			},
			{field : 'audit',title : '开票资料审核状态',	width : 40,align : 'center',hidden : false,
				formatter: function(value,row,index){
					if(value == 0){
						return '待审核';
					}else if(value == 1){
						return '已审核';
					}else if(value == 2){
						return '未通过';
					}else{
						return '未知';
					}
				},
			},
			{field : 'cz',title : '操作',	width : 50,align : 'center',hidden : false,
				formatter: function(value,row,index){
				    return '<a href="#" style="color:blue;margin-left:3px;" onClick="showTicket('+row.id+')">查看</a>';
				}
			}
		] ],
		toolbar : [ {
			text : '删除',
			iconCls : 'icon-cancel',
			handler : function() {
				deleteTicket();
			}		
		}, ]
	});
}

/**
 * 查询发票列表
 * @param haveparams 是否有参数，初试加载传入false，点击查询按钮传入true
 */
function selectItemListChecked(haveparams){
	var params = {};
	if(haveparams){
		var buyerName=$.trim($("#list_buyerName_checked").val());          					//会员名
		var addTime_begin=$.trim($('#list_addTime_begin_checked').datetimebox('getValue'));     //添加时间 开始
		var addTime_end=$.trim($('#list_addTime_end_checked').datetimebox('getValue'));      	//添加时间 结束	
		var status = $.trim($("#status_checked").val());
		params = {
			buyerName : buyerName,
			addTime_begin : addTime_begin,
			addTime_end : addTime_end,
			status : status
		};
	}else{
		var status = $.trim($("#status_checked").val());
		params = {
				status : status
			};	
	}
	$("#ticketList_dateGrid_checked").datagrid_pagination($("#ticketList_dateGrid_checked"),path+"/ticket/v1/list",params,"","");
}

/**
 * 查看开票信息
 */
function showTicket(id){
	if(id != null && id != ''){
		var params = {id:id};
    	$.ajax({
    		type:"post",
    		//contentType:"application/json; charset=utf-8",
    		url:path+'/ticket/v1/showTicket',
    		data:params,
    		dataType:"json",
    		success:function(data){
    			$('#id_checked').val(data.id);
    			if(data.audit == 0){
    				$('#ticketStatus_checked').val("未审核");
    			}else if(data.audit == 1){
    				$('#ticketStatus_checked').val("已审核");
    			}else{
    				$('#ticketStatus_checked').val("未通过");
    			}
    			$('#ticketCompanyName_checked').val(data.company);
    			$('#ticketOrderNum_checked').val(data.taxno);
    			$('#ticketAddress_checked').val(data.address);
    			$('#ticketPhone_checked').val(data.mobile);
    			$('#ticketBankName_checked').val(data.bank);
    			$('#ticketBankNo_checked').val(data.bankno);
    			$('#ticketFile_checked').val("一般纳税人资料证明文件");
    			$('#download_checked').attr("href","/ticket/v1/download?id=" + data.id);
    			$('#ticketReceiver_checked').val(data.receiver);
    			$('#ticketReceiverPhone_checked').val(data.receiverPhone);
    			$('#ticketReceiverAddress_checked').val(data.province + " " + data.city + " " + data.area + " " + data.receiverAddress);
    			$('#ticketCreateDate_checked').val(new Date(data.createdate).format("yyyy-MM-dd hh:mm:ss"));
    		}
       });
	}
	$('#itemTicketWindow_checked').window("open");
}

/*
 * 审核开票
 */
function checkTicket(){
	var id = $('#id').val();
	$.messager.defaults = { ok: "通过", cancel: "驳回" };  
	$.messager.confirm('确认', '确定要通过改会员的开票资料吗？', function(r){
		if(r){
			var params = {id:id,flag:1};
	    	$.ajax({
	    		type:"post",
//	    		contentType:"application/json; charset=utf-8",
	    		url:path+'/ticket/v1/checkTicket',
	    		data:params,
	    		dataType:"json",
	    		success:function(data){
	    			if(data.result == 1){
	    				$.messager.alert('提示','审核成功：已通过',undefined,function(){
	    					$('#itemTicketWindow_checked').window("close");
	    					selectItemListChecked(false);
	    				});
	    			}else{
	    				$.messager.alert('提示',data.msg);
	    			}
	    		}
	       });
		}else{
			var params = {id:id,flag:2};
	    	$.ajax({
	    		type:"post",
//	    		contentType:"application/json; charset=utf-8",
	    		url:path+'/ticket/v1/checkTicket',
	    		data:params,
	    		dataType:"json",
	    		success:function(data){
	    			if(data.result == 1){
	    				$.messager.alert('提示','审核成功：未通过',undefined,function(){
	    					$('#itemTicketWindow_checked').window("close");
	    					selectItemListChecked(false);
	    				});
	    			}else{
	    				$.messager.alert('提示',data.msg);
	    			}
	    		}
	       });
		}
	});
	
}

/**
 * 删除开票资料
 */
function deleteTicket(){
	var id = getSelectionsIds();
	if(id.length == 0){
		$.messager.alert('提示','未选中开票资料!');
		return ;
	}
	$.messager.confirm('确认','确定删除此开票资料吗？',function(r){
	    if (r){
	    	var params = {id:id};
	    	$.ajax({
	    		type:"post",
	    		contentType:"application/json; charset=utf-8",
	    		url:path+'/ticket/v1/deleteTicket',
	    		data:id+"",
	    		dataType:"json",
	    		success:function(data){
	    			if(data.result == 1){
	    				$.messager.alert('提示','删除开票资料成功!',undefined,function(){
	    					selectItemListChecked(false);
	    				});
	    			}else{
	    				$.messager.alert('提示',data.msg);
	    			}
	    		}
	       });
	    }
	});
}

/**
 * 查询所有品牌（用于下拉菜单）
 */
function selectItemBrands(){
	$.ajax({
		type:"post",
		contentType:"application/json; charset=utf-8",
		url:path+'/item/v1/selectitembrand?page=1&rows=10000',
		data:'{}',
		dataType:"json",
		success:function(data){
			$("#list_brandId").combobox({
				valueField : 'id',
				textField : 'brandName',
				data : data.rows
			});
		}
   });
}

/**
 * 查询所有类别（用于下拉树）
 */
function selectItemCatTree(){
	$.ajax({
		type:"post",
		contentType:"application/json; charset=utf-8",
		url:path+'/item/v1/cat/list',
		data:'{}',
		dataType:"json",
		success:function(data){
			$('#list_categoryId').combotree('loadData', data);
		}
   });
}

/**
 * 得到所选择行的id（int型）
 * @returns {Array}
 */
function getSelectionsIds(){
	var itemList = $("#ticketList_dateGrid_checked");
	var sels = itemList.datagrid("getSelections");
	var ids = new Array();
	for(var i in sels){
		ids.push(parseInt(sels[i].id));
	}
	return ids;
}




